<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\common\model\goods\Goods;
use app\common\model\MoneyLog;
use app\common\model\order\Goods as OrderGoods;
use app\common\model\order\Order;
use app\common\model\User as ModelUser;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {

        $array[] = ['商品名称', '销量', '销售额'];
        $time =  request()->post('time', '');
        if (isset($time) && $time) {
            $date = explode(' - ', $time);
            $where['paytime'] = ['between', [strtotime($date[0]), strtotime($date[1] . " 23:59:59")]];
        }
        $where['order_status'] = ['>', '0'];
        $goods_ids = Goods::where(['status' => 'normal'])->order('sales desc')->column('id');
        $order_ids = Order::where($where)->column('id');
        $goods = OrderGoods::whereIn('order_id', $order_ids)->whereIn('goods_id', $goods_ids)->field('goods_name,sum(goods_num) as total_num,sum(subtotal) as total')->order('total_num desc')->group('goods_id')->limit(10)->select();
        foreach ($goods as $k => $v) {
            $array[] = [$v['goods_name'], $v['total_num'], $v['total']];
        }
        $this->view->assign([
            'income_today'         => Order::whereTime('paytime', 'today')->where('order_status', '>', '0')->sum('pay_money'),
            'income_month'         => Order::whereTime('paytime', 'year')->where('order_status', '>', '0')->sum('pay_money'),
            'income_total'         => Order::where('order_status', '>', '0')->sum('pay_money'),
            'user_count'                 => ModelUser::where('group_id', '1')->count(),
            'level_count'                 => ModelUser::where('group_id', '2')->count(),
            'team_count'                 => ModelUser::where('group_id', '3')->count(),
            'director_count'                 => ModelUser::where('group_id', '4')->count(),
            'economize_count'                 => ModelUser::where('group_id', '5')->count(),
            'total_sales'                 => Order::where('order_status', '>', '0')->sum('total_num'),
            'total_sales_volume'                 => Order::where('order_status', '>', '0')->sum('pay_money'),
            'today_commission'                 => MoneyLog::whereIn('memo',['直推奖','间推奖','分红奖'])->whereTime('createtime', 'today')->sum('money'),
            'month_commission'                 => MoneyLog::whereIn('memo',['直推奖','间推奖','分红奖'])->whereTime('createtime', 'month')->sum('money'),
            'year_commission'                 => MoneyLog::whereIn('memo',['直推奖','间推奖','分红奖'])->whereTime('createtime', 'year')->sum('money'),
        ]);

        // $this->assignconfig('goods', json_encode($goods));
        $this->assignconfig('goods', $array);
        $this->view->assign('time', $time ?? '');
        return $this->view->fetch();
    }
}
